import type { App } from "vue";
import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },
  {
    path: "/selectShop",
    component: () => import("@/views/login/selectShop.vue"),
    meta: { hidden: true },
  },
  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "dashboard",
        meta: {
          title: "dashboard",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
    ],
  },
];

// 动态路由
export const asyncRouterMap: RouteRecordRaw[] = [
  {
    path: "/generalSituation",
    component: Layout,
    redirect: "/generalSituation/dataBoard",
    name: "generalSituation",
    meta: {
      title: "generalSituation",
      icon: "el-icon-setUp",
      hidden: false,
      keepAlive: true,
      alwaysShow: true,
    },
    children: [
      {
        path: "dataBoard",
        component: () => import("@/views/generalSituation/dataBoard/index.vue"),
        name: "dataBoard",
        meta: {
          title: "dataBoard",
          icon: "el-icon-pointer",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
        },
      },
      {
        path: "trend",
        component: () => import("@/views/generalSituation/trend/index.vue"),
        name: "trend",
        meta: {
          title: "trend",
          icon: "el-icon-pointer",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
        },
      },
    ],
  },
];
/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHistory("/dataPlatform"),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

/**
 * 配置并挂载路由到Vue应用实例
 *
 * @param app - Vue应用实例，用于挂载路由配置
 *             （类型：App<Element>，表示使用Element类型的根组件）
 * @returns 无返回值（void），直接修改传入的应用实例
 */
export function setupRouter(app: App<Element>) {
  // 将预定义的路由配置注入到Vue应用中
  app.use(router);
}

export default router;
